package com.kabigon.tiny.service;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.tiny.service
 * @data 2023-07-06 0:15
 */
public class excel {

    public static void main(String[] args) {
        /*// 创建工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建工作表
        Sheet sheet = workbook.createSheet("Sheet1");

        // 创建行
        Row row = sheet.createRow(0);

        // 创建单元格并设置值
        Cell cell1 = row.createCell(0);
        cell1.setCellValue("Hello");

        Cell cell2 = row.createCell(1);
        cell2.setCellValue("World");

        // 导出Excel文件
        try (FileOutputStream fileOut = new FileOutputStream("example.xlsx")) {
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭工作簿
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Excel文件导出成功！");*/

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建第一个工作表
        Sheet sheet1 = workbook.createSheet("Sheet1");

        // 创建行和单元格
        Row row1 = sheet1.createRow(0);
        Cell cellA1 = row1.createCell(0);
        cellA1.setCellValue("Header 1");

        Row row2 = sheet1.createRow(1);
        Cell cellA2 = row2.createCell(0);
        cellA2.setCellValue("Data 1");

        // 合并单元格
        sheet1.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));

        // 设置样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellA1.setCellStyle(headerStyle);

        CellStyle dataStyle = workbook.createCellStyle();
        dataStyle.setAlignment(HorizontalAlignment.CENTER);
        cellA2.setCellStyle(dataStyle);

        // 创建第二个工作表
        Sheet sheet2 = workbook.createSheet("Sheet2");

        // 创建行和单元格
        Row row3 = sheet2.createRow(0);
        Cell cellA3 = row3.createCell(0);
        cellA3.setCellValue("Header 2");

        Row row4 = sheet2.createRow(1);
        Cell cellA4 = row4.createCell(0);
        cellA4.setCellValue("Data 2");

        // 导出Excel文件
        try (FileOutputStream fileOut = new FileOutputStream("complex_example.xlsx")) {
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭工作簿
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Excel文件导出成功！");
    }
}

